package com.zhuanzhuan.hero.user.service;

import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.youpin.common.utils.json.JsonUtils;
import com.bj58.zhuanzhuan.zzpassport.contract.IZZCancelAccountService;
import com.bj58.zhuanzhuan.zzpassport.entity.CheckCanCancelAccountResult;
import com.bj58.zhuanzhuan.zzpassport.entity.CheckCancelAccountParam;
import com.zhuanzhuan.hero.member.response.MemberDTO;
import com.zhuanzhuan.hero.user.entity.HeroUserInfo;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.enums.UserStateEnum;
import com.zhuanzhuan.hero.user.helper.HeroMemberHelper;
import com.zhuanzhuan.hero.user.helper.HeroRecycleOrderHelper;
import com.zhuanzhuan.hero.user.helper.HeroSocialHelper;
import com.zhuanzhuan.hero.user.mq.producer.HeroUserProducer;
import com.zhuanzhuan.hero.user.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName CancelAccountFacade
 * @Description  这个类对的全路径不可变   全路径配置在用户中台执行注销校验回调的逻辑   联系人：朱建超
 * @Author hanjunjun
 * @Date 2024/7/12 13:56
 */
@ServiceBehavior
@Slf4j
public class HeroCancelAccountService implements IZZCancelAccountService {

    @Resource
    private HeroUserInfoService userInfoService;

    @Resource
    private HeroMemberHelper heroMemberHelper;

    @Resource
    private HeroRecycleOrderHelper heroRecycleOrderHelper;

    @Resource
    private HeroSocialHelper heroSocialHelper;

    @Resource
    private IUserService userService;

    @Resource
    private HeroUserProducer heroUserProducer;

    @Override
    public CheckCanCancelAccountResult checkCanCancel(String s, CheckCancelAccountParam checkCancelAccountParam) {
        log.info("checkCanCancel param={}", JsonUtils.toJsonWithoutNull(checkCancelAccountParam));
        Long uid = checkCancelAccountParam.getUid();
        Assert.notNull(uid, ExceptionMsgEnum.PARAM_ERROR);

        //是否已注销
        HeroUserInfo userInfo = userInfoService.getUserByUid(uid);
        //如果不存在，则不是侠客汇用户
        if (Objects.isNull(userInfo)) {
            log.info("{}不是侠客汇用户", uid);
            return CheckCanCancelAccountResult.generateSuccessResult(true, null);
        }

        if (UserStateEnum.UNREGISTERED.getCode().equals(userInfo.getUserState())) {
            log.info("用户已是注销状态 uid={}", uid);
            return CheckCanCancelAccountResult.generateSuccessResult(false, Collections.singletonList("请勿重复注销"));
        }

        List<String> failMsg = new ArrayList<>();
        //是否为会员
        MemberDTO memberDTO = heroMemberHelper.queryMember(uid);
        if (Objects.nonNull(memberDTO)) {
            if (heroMemberHelper.isMemberUnregist(memberDTO)) {
                log.info("还是会员，不能注销 uid={}", uid);
                failMsg.add("请退出侠客汇会员后再进行注销。");
            }
        }

        //回收单校验
        if (heroRecycleOrderHelper.hasCompletedOrderInSevenDays(uid)) {
            log.info("回收单完成未超过7天，无法注销 uid={}", uid);
            failMsg.add("您的回收单完成未超过7天，无法注销。");
        }
        if (heroRecycleOrderHelper.hasUnfinishedOrder(uid)) {
            log.info("回收单尚未完成，无法注销 uid={}", uid);
            failMsg.add("您的回收单尚未完成，无法注销。");
        }
        if (CollectionUtils.isNotEmpty(failMsg)) {
            return CheckCanCancelAccountResult.generateSuccessResult(false, failMsg);
        }

        return CheckCanCancelAccountResult.generateSuccessResult(true, null);
    }

    /**
     * 注销
     *
     * @param uid
     * @return boolean
     * @author  hanjunjun
     * @date 2024/7/12
     **/
    public boolean doCancelAccount(Long uid) {
        log.info("doCancelAccount uid={}", uid);

        //如果不存在，则不是侠客汇用户
        HeroUserInfo userInfo = userInfoService.getUserByUid(uid);
        if (Objects.isNull(userInfo)) {
            log.info("{}不是侠客汇用户", uid);
            return true;
        }
        if (UserStateEnum.UNREGISTERED.getCode().equals(userInfo.getUserState())) {
            log.info("用户已是注销状态 uid={}", uid);
            return true;
        }

        //注册逆逻辑
        boolean userResult = userService.doUnRegist(uid);
        log.info("doCancelAccount userResult={}", userResult);

        //社交模块
        boolean socialResult = heroSocialHelper.cancelAccount(uid);
        log.info("doCancelAccount socialResult={}", socialResult);

        return userResult && socialResult;
    }
}
